package top.zhongyy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.core.appender.FailoversPlugin;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.DispatcherServlet;
import top.zhongyy.exception.NotFoundException;
import top.zhongyy.mapper.BlogTagsMapper;
import top.zhongyy.mapper.TagMapper;
import top.zhongyy.mapper.TypeMapper;
import top.zhongyy.pojo.Blog;
import top.zhongyy.mapper.BlogMapper;
import top.zhongyy.pojo.BlogTags;
import top.zhongyy.pojo.Tag;
import top.zhongyy.pojo.Type;
import top.zhongyy.service.BlogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import top.zhongyy.service.BlogTagsService;
import top.zhongyy.util.MarkdownUtils;
import top.zhongyy.vo.BlogQuery;

import javax.servlet.Servlet;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端博客服务实现类
 * </p>
 *
 * @author Andy
 * @since 2021-03-25
 */
@Service
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog> implements BlogService {
    
    @Autowired
    private BlogMapper blogMapper;
    
    @Autowired
    private TypeMapper typeMapper;
    
    @Autowired
    private BlogTagsMapper blogTagsMapper;
    
    @Autowired
    private TagMapper tagMapper;
    
    @Autowired
    private BlogTagsService blogTagsService;
    
    @Override
    public List<Blog> blogListPage() {
        List<Blog> blogs = blogMapper.selectList(null);
        for (Blog blog : blogs) {
            blog.setType(typeMapper.selectById(blog.getTypeId()));
        }
        return blogMapper.getBlogList();
    }
    
    /**
     * @param blogQuery
     * @Desc: 博客模糊查询
     * @author Andy
     * @date: 2021/3/26 16:18
     */
    @Override
    public List<Blog> searchBlog(BlogQuery blogQuery) {
        QueryWrapper<Blog> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(blogQuery.getTitle())) {
            wrapper.like("title", blogQuery.getTitle());
        }
        if (blogQuery.isRecommend()) {
            wrapper.eq("recommend", blogQuery.isRecommend());
        }
        if (blogQuery.getTypeId() != null) {
            wrapper.eq("type_id", blogQuery.getTypeId());
        }
        return blogMapper.selectList(wrapper);
    }
    
    /**
     * @param blog
     * @Desc: 博客的保存
     * @author Andy
     * @date: 2021/3/26 16:18
     */
    @Override
    public int saveBlog(Blog blog) {
        int insert = blogMapper.insert(blog);
        blog.setUpdateTime(blog.getCreateTime());
        String[] split = blog.getTagIds().split(",");
        for (String s : split) {
            blogTagsMapper.insert(new BlogTags(blog.getId(), Long.parseLong(s)));
        }
        return insert;
    }
    
    @Override
    public int deleteBlog(Long id) {
        int delete = blogMapper.deleteById(id);
        blogTagsService.deleteById(id);
        return delete;
    }
    
    @Override
    public int updateBlog(Blog blog) {
        blogTagsService.deleteById(blog.getId());
        String[] split = blog.getTagIds().split(",");
        for (String s : split) {
            blogTagsMapper.insert(new BlogTags(blog.getId(), Long.parseLong(s)));
        }
        int update = blogMapper.updateById(blog);
        return update;
    }
    
    @Override
    public List<Blog> getBlogList() {
        return blogMapper.getBlogList();
    }
    
    @Override
    public List<Blog> getRecommend(int size) {
        return blogMapper.recommendBlog(size);
    }
    
    @Override
    public List<Blog> searchBlog(String query) {
        return blogMapper.indexSearchBlog("%" + query + "%");
    }
    
    @Override
    public Blog getAndConvert(Long id) {
        Blog blog = convertIdsToTags(blogMapper.getOneBlog(id));
        if (blog == null) {
            throw new NotFoundException("该博客不存在");
        }
        Blog b = new Blog();
        BeanUtils.copyProperties(blog, b);
        b.setContent(MarkdownUtils.markdownToHtmlExtensions(blog.getContent()));
        blogMapper.updateViews(id);
        return b;
    }
    
    @Override
    public List<Blog> getBlogByType(Long typeId) {
        return blogMapper.getBlogByType(typeId);
    }
    
    @Override
    public List<Blog> getBlogByTag(Long tagId) {
        return blogMapper.getBlogByTag("%" + tagId + "%");
    }
    
    @Override
    public List<Blog> getAdminBlogList() {
        return blogMapper.getAdminBlogList();
    }
    
    public Blog convertIdsToTags(Blog blog) {
        String[] split = blog.getTagIds().split(",");
        List<Tag> tags = new ArrayList<>();
        for (String s : split) {
            tags.add(tagMapper.selectById(s));
        }
        blog.setTags(tags);
        return blog;
    }
    
    
}
